#include "ui.h"

static int DefaultOnDraw(Button *ptButton, PDispBuff ptDispBuff)
{
	/* 绘制底色 */
	DrawRegion(&ptButton->tRegion, BUTTON_DEFAULT_COLOR);

	/* 居中写文字 */
	DrawTextInRegionCentral(ptButton->name, &ptButton->tRegion, BUTTON_TEXT_COLOR);

	/* flush to lcd/web */
	FlushDeviceRegion(&ptButton->tRegion, ptDispBuff);

	return 0;
}


static int DefaultOnPressed(Button *ptButton, PDispBuff ptDispBuff, PInputEvent ptInputEvent)
{
	ptButton->status = !ptButton->status; // 按下按钮之后按钮状态发生变化

	// 根据ptButton的status决定用什么颜色来绘制按钮底色
	DrawRegion( &ptButton->tRegion, (ptButton->status == STATUS_DEFAULT) ? BUTTON_DEFAULT_COLOR : BUTTON_PRESSED_COLOR );

	// 根据ptButton的status决定用什么颜色来绘制按钮底色
	DrawRegion( &ptButton->tRegion, (ptButton->status == STATUS_DEFAULT) ? BUTTON_DEFAULT_COLOR : BUTTON_PRESSED_COLOR );
	/* 居中写文字 */
	DrawTextInRegionCentral(ptButton->name, &ptButton->tRegion, BUTTON_TEXT_COLOR);

	/* flush to lcd/web */
	FlushDeviceRegion(&ptButton->tRegion, ptDispBuff);
	return 0;
}


// 按照其他四个传入的参数，对tButton的字段值进行设置
int InitButton(PButton ptButton, char *name, PRegion ptRegion, ONDRAW_FUNC OnDraw, ONPRESSED_FUNC OnPressed)
{   
    ptButton->status = STATUS_DEFAULT;
    ptButton->name = name;
    ptButton->tRegion = *ptRegion;
    ptButton->OnDraw = OnDraw ? OnDraw : DefaultOnDraw; // 没有传入OnDraw函数就传入DefaultOnDraw
    ptButton->OnPressed = OnPressed ? OnPressed : DefaultOnPressed;
    return 0;
}

